package yl.hs.bmipfull.service.host.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.host.IProductionLineRawConfigDao;
import yl.hs.bmipfull.pojo.host.ProductionLineRawConfigEntity;
import yl.hs.bmipfull.service.host.IProductionLineRawConfigService;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  生产线主机原材仓位对应设置服务层接口实现<br/>
 * 创建时间: 2021年06月28日 14时45分23秒 <br/>
 *
 * @author 谢东
 */
@Service
public class ProductionLineRawConfigServiceImpl extends SystemBaseService implements IProductionLineRawConfigService {

    @Autowired
    IProductionLineRawConfigDao dao;

    @Override
    public List<ProductionLineRawConfigEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductionLineRawConfigEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<ProductionLineRawConfigEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductionLineRawConfigEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<ProductionLineRawConfigEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public ProductionLineRawConfigEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveProductionLineRawConfig(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductionLineRawConfigEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProductionLineRawConfigEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            //entity.setSn(queryNewSN(ProductionLineRawConfigEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getPlSn(), "插入生产线主机原材仓位对应设置信息", Tl.toJson(entity), ProductionLineRawConfigEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新生产线主机原材仓位对应设置信息", Tl.toJson(entity), ProductionLineRawConfigEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {

        return 0;
    }
}